package com.polimi.meteocal.business.entity;

import java.io.Serializable;

import javax.persistence.*;

import java.util.List;


/**
 * The persistent class for the users database table.
 * 
 */
@Entity
@Table(name="users")
@NamedQueries({

@NamedQuery(name="User.findAll", query=""
		+ "SELECT u FROM User u"),

@NamedQuery(name="User.findForMail", query=""
		+ "SELECT u FROM User u "
				+ "WHERE u.email= :email"),

@NamedQuery(name="User.findPartecipantsByEventId", query="SELECT u FROM User u, Event e, Ack a "
		  + "WHERE e.id = :eventId AND "
				+ "e.id = a.id.eventId AND "
				+ "a.id.ackSender = u.email AND "
				+ "a.response='ACCEPT' "),
				      
@NamedQuery(name="User.findOrganizersByEventId",query=""
		+ "SELECT u FROM User u JOIN u.events e "
				+ "WHERE u.events = :event"),
})
public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	public static final String FIND_ALL = "User.findAll";
	public static final String FIND_FOR_MAIL = "User.findForMail";
	public static final String FIND_PARTECIPANTS_BY_EVENT_ID = "User.findPartecipantsByEventId";
	public static final String FIND_ORGANIZERS_BY_EVENT_ID = "User.findOrganizersByEventId";
	
	@Id
	private String email;

	private String groupname;

	private String name;

	private String password;

	private boolean publicvisibility;

	private String surname;

	//bi-directional many-to-one association to Invite
	@OneToMany(mappedBy="user1")
	private List<Invite> invites1;

	//bi-directional many-to-one association to Invite
	@OneToMany(mappedBy="user2")
	private List<Invite> invites2;

	//bi-directional many-to-many association to Event
	@ManyToMany
	@JoinTable(
		name="organizer"
		, joinColumns={
			@JoinColumn(name="USER_MAIL")
			}
		, inverseJoinColumns={
			@JoinColumn(name="EVENT_ID")
			}
		)
	private List<Event> events;

	//bi-directional many-to-one association to Notification
	@OneToMany(mappedBy="user")
	private List<Notification> notifications;

	public User() {
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getGroupname() {
		return this.groupname;
	}

	public void setGroupname(String groupname) {
		this.groupname = groupname;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean getPublicvisibility() {
		return this.publicvisibility;
	}

	public void setPublicvisibility(boolean publicvisibility) {
		this.publicvisibility = publicvisibility;
	}

	public String getSurname() {
		return this.surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public List<Invite> getInvites1() {
		return this.invites1;
	}

	public void setInvites1(List<Invite> invites1) {
		this.invites1 = invites1;
	}

	public Invite addInvites1(Invite invites1) {
		getInvites1().add(invites1);
		invites1.setUser1(this);

		return invites1;
	}

	public Invite removeInvites1(Invite invites1) {
		getInvites1().remove(invites1);
		invites1.setUser1(null);

		return invites1;
	}

	public List<Invite> getInvites2() {
		return this.invites2;
	}

	public void setInvites2(List<Invite> invites2) {
		this.invites2 = invites2;
	}

	public Invite addInvites2(Invite invites2) {
		getInvites2().add(invites2);
		invites2.setUser2(this);

		return invites2;
	}

	public Invite removeInvites2(Invite invites2) {
		getInvites2().remove(invites2);
		invites2.setUser2(null);

		return invites2;
	}

	public List<Event> getEvents() {
		return this.events;
	}

	public void setEvents(List<Event> events) {
		this.events = events;
	}

	public List<Notification> getNotifications() {
		return this.notifications;
	}

	public void setNotifications(List<Notification> notifications) {
		this.notifications = notifications;
	}

	public Notification addNotification(Notification notification) {
		getNotifications().add(notification);
		notification.setUser(this);

		return notification;
	}

	public Notification removeNotification(Notification notification) {
		getNotifications().remove(notification);
		notification.setUser(null);

		return notification;
	}

}